package cn.com.ddapp.ddm.ui.mine;

import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.Entity;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.TimeCount;
import cn.com.ddapp.ddm.util.ToolFor9Ge;

public class BindMobileActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.titletext)
    TextView titletext;
    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.get_verify_code)
    TextView getVerifyCode;
    @BindView(R.id.verify_code)
    EditText etVerifyCode;
    @BindView(R.id.bind)
    Button bind;

    String mobile,code;
    OkUtilCallback callback;
    String userId ;

    TimeCount timeCount;

    @Override
    public void initView() {
        titletext.setText(R.string.bindmobile);

    }

    @Override
    public void initData() {
        userId= SPUtils.getStringData(this, SPUtils.USERID);
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_bind_mobile;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }

    @OnClick({R.id.get_verify_code,R.id.bind,R.id.back })
    public void onClick(View view){
        switch (view.getId()){
            case R.id.get_verify_code :
                mobile = String.valueOf(etMobile.getText());
                if (!ToolFor9Ge.isPhoneNO(mobile)){
                    BaseApplication.getIntstance().showToast("请输入正确的电话号码", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                }else {
                    callback = new OkUtilCallback() {
                        @Override
                        public void onMyResponse(String response, int id) {
                            Gson gson = new Gson();
                            Entity entity = gson.fromJson(response, Entity.class);
                            if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult())) && entity.getLogin()) {
                                BaseApplication.getIntstance().showToast("请检查网络", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                            } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                            } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                BaseApplication.getIntstance().showToast("验证码已发送", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                                timeCount.start();
                            }
                        }
                    };
                    //发送验证码
                    OKUtilAPI.post(OKUtilAPI.sendVerifyCode, "key=" + mobile, callback);


                }
                break;
            case R.id.bind :
                code = String.valueOf(etVerifyCode.getText());
                if ("".equals(mobile)){
                    BaseApplication.getIntstance().showToast("请输入电话号码", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                }else if("".equals(code)){
                    BaseApplication.getIntstance().showToast("验证码不能为空", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                }else {
                    callback = new OkUtilCallback() {
                        @Override
                        public void onMyResponse(String response, int id) {
                            Gson gson = new Gson();
                            Entity entity = gson.fromJson(response, Entity.class);
                            if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult())) && entity.getLogin()) {
                                BaseApplication.getIntstance().showToast("请检查网络", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                            } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                            } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                BaseApplication.getIntstance().showToast("绑定成功", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                                finish();
                            }
                        }
                    };
                    //绑定手机
                    OKUtilAPI.post(OKUtilAPI.bindMobile,"userId="+userId+ "&mobile=" + mobile+"&randCode="+code, callback);
                }
                break;
            case R.id.back :
                finish();
                break;
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        timeCount = new TimeCount(60000, 1000, getVerifyCode);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        timeCount.cancel();
    }
}
